// page_two/qpqzaddweib/qpqzaddweib.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    heights: "",
    title: '维保记录',
    buttonClicked: false,
    sbxhindex: 0,
    sbxhlist: ['型号1', '型号2'],
    sbxh: '请选择设备型号',
    sbxhid: 0,
    date: '请选择检测日期',
    jcsx: '',
    jcjg: '',
    photo: [],
    wuphoto: ''
  },
  // 设备型号列表获取
  getsbxh() {
    let that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'Gasstation/gasstation_all', {
      shopId: wx.getStorageSync('shopId')
    }, 'POST').then(res => {
      wx.hideLoading();
      if (res.data.code == 200) {
        let arr = res.data.data;
        let sbxhlist = [];
        arr.forEach(item => {
          sbxhlist.push({
            id: item.id,
            name: item.models
          })
        })
        this.setData({
          sbxhlist 
        })
      }
    })
  },
  // 设备型号
  sbxh(e) {
    let index = e.detail.value
    this.setData({
      sbxh: this.data.sbxhlist[index].name,
      sbxhid: this.data.sbxhlist[index].id
    });
  },
  // 检测日期
  date: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  // 检测事项
  jcsx(e) {
    this.setData({
      jcsx: e.detail.value
    })
  },
  // 检测结果
  jcjg(e) {
    this.setData({
      jcjg: e.detail.value
    })
  },
  // 上传
  chuan(e) {
    var that = this
    var pics = [];
    var wuphoto = this.data.wuphoto;
    this.myChooseImage(9).then(res => {
      const tempFilePaths = res.tempFilePaths
      for (var i = 0; i < tempFilePaths.length; i++) {
        pics.push(tempFilePaths[i])
      }
      if ((pics.length) + (that.data.photo.length) > 9) {
        wx.showToast({
          title: '最多可上传' + 9 + "张",
          icon: 'none'
        })
      } else {
        for (var i = 0; i < tempFilePaths.length; i++) {
          (function (i) { // 注意这里是形参
            setTimeout(function () {
              that.duoupimg(tempFilePaths[i]).then(res => {
                wx.showLoading({
                  title: '上传图片中...',
                });
                var data = JSON.parse(res.data)
                var urls = data.httpUrl + data.imgUrl
                wuphoto = wuphoto.concat(data.imgUrl + ",")
                that.setData({
                  photo: that.data.photo.concat(urls),
                  wuphoto: wuphoto
                });
                if (i == tempFilePaths.length - 1) {
                  wx.hideLoading();
                }
              })
            }, 800 * i); // 还是每秒执行一次，不是累加的
          })(i) // 注意这里是实参，这里把要用的参数传进去
        }
      }
    })
  },
  // 差掉
  chadiao(e) {
    var index = e.currentTarget.dataset.index
    var photo = this.data.photo;
    var wuphoto = this.data.wuphoto;
    var dd = wuphoto.split(",")
    console.log(dd)
    photo.splice(index, 1)
    dd.splice(index, 1)
    var rr = dd.join(",")
    this.setData({
      photo: photo,
      wuphoto: rr
    })
  },
  fangda(e) {
    var img = e.currentTarget.dataset.img
    var list = e.currentTarget.dataset.list
    wx.previewImage({
      current: img,
      urls: list
    })
  },
  // 验证
  ti() {
    if (this.data.sbxh == '请选择设备型号') {
      wx.showToast({
        title: '请选择设备型号',
        icon: 'none'
      })
    } else if (this.data.date == '请选择检测日期') {
      wx.showToast({
        title: '请选择检测日期',
        icon: 'none'
      })
    } else if (this.data.jcsx == '') {
      wx.showToast({
        title: '请输入检测事项',
        icon: 'none'
      })
    } else if (this.data.jcjg == '') {
      wx.showToast({
        title: '请输入检测结果',
        icon: 'none'
      })
    } else if (this.data.photo.length == 0) {
      wx.showToast({
        title: '请上传维保照片',
        icon: 'none'
      })
    } else {
      this.que()
    }
  },
  // 添加消防设备
  que() {
    let that = this
    that.setData({
      buttonClicked: true
    })
    this.myRequest(yuming + 'Gasstation/gasstation_weibao_add', {
      shopId: wx.getStorageSync('shopId'),
      pid: that.data.sbxhid,
      jiance_time: that.data.date,
      pcontent: that.data.jcsx,
      jiance_result: that.data.jcjg,
      images: that.data.wuphoto
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        setTimeout(function () {
          wx.navigateBack({
            delta: 0
          })
        }, 1000)
      } else {
        that.setData({
          buttonClicked: false
        })
      }
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5
    })
    this.getsbxh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
})